package com.example.sys.service;

import com.example.common.exception.BizException;
import com.example.common.exception.errcode.ErrCodeLogin;
import com.example.common.result.PageResult;
import com.example.common.utils.BeanUtils;
import com.example.common.utils.IdUtils;
import com.example.sys.dao.SysUserDao;
import com.example.sys.dao.SysUserRepository;
import com.example.sys.entity.dto.UserDto;
import com.example.sys.entity.po.SysUser;
import com.example.sys.entity.query.UserQuery;
import com.example.sys.entity.vo.SysUserInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserService {
    @Resource
    private IdUtils idUtils;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private SysUserRepository sysUserRepository;
    @Resource
    private SysUserDao sysUserDao;

    public void insertUser(UserDto userDto) {

        SysUser sysUser = BeanUtils.facade().map(userDto, SysUser.class);
        {
            sysUser.setUserId(idUtils.nextId());
            sysUser.setUserPwd(passwordEncoder.encode(sysUser.getUserPwd() == null ? "123456" : sysUser.getUserPwd()));     //密码没有设置就使用初识密码 123456,有就加密
            sysUser.setStatus(1);
            sysUser.setShowOrder(sysUserRepository.findFirstByOrderByShowOrderDesc().getShowOrder() + 1);
            sysUser.setCreateTime(LocalDateTime.now());
        }
        sysUserRepository.save(sysUser);
    }

    public void deleteSysUserByUserId(Long userId) {
        if (userId.longValue() == 0) {
            throw new BizException(ErrCodeLogin.ADMIN_CANNOT_DELETE_EXCEPTION); //系统用户不可以删除
        }
        SysUser sysUser = SysUser.builder()
                .userId(userId)
                .status(0)
                .updateTime(LocalDateTime.now())
                .build();
        sysUserRepository.save(sysUser);
    }

    public void updateSysUserByUserId(UserDto userDto) {
        SysUser sysUser = null;
        //查询手机号
        sysUser = sysUserRepository.findByIphoneNo(userDto.getIphoneNo());
        if (sysUser != null) {
            throw new BizException(ErrCodeLogin.IPHONE_REGISTERED_ERROR);   //手机号已经注册
        }
        //查询邮箱
        sysUser = sysUserRepository.findByEmailAddress(userDto.getEmailAddress());
        if (sysUser != null) {
            throw new BizException(ErrCodeLogin.EMAIL_REGISTERED_ERROR);   //邮箱已经注册
        }

        sysUser = BeanUtils.facade().map(userDto, SysUser.class);   //对象转换
        sysUser.setUserPwd(passwordEncoder.encode(sysUser.getUserPwd()));
        sysUser.setUpdateTime(LocalDateTime.now());
        sysUserRepository.save(sysUser);
    }

    public SysUserInfoVo selectSysUserByUserId(Long userId) {
        SysUser sysUser = sysUserRepository.findById(userId).get();
        sysUser.setUserPwd("");
        SysUserInfoVo sysUserInfoVo = BeanUtils.facade().map(sysUser, SysUserInfoVo.class);
        return sysUserInfoVo;
    }

    public PageResult<SysUserInfoVo> selectSysUserList(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPage(), userQuery.getRows());

        List<SysUser> list = sysUserDao.selectSysUserInfoList(userQuery);
        list.forEach(i -> i.setUserPwd("******"));
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);

        return PageResult.result(pageInfo.getTotal(), BeanUtils.facade().mapAsList(pageInfo.getList(), SysUserInfoVo.class));
    }
}
